package com.imooc.ad.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 * @Description: TODO
 * @ClassName: PreRequestFilter
 * @Author: zhangzhongyu
 * @Date: 2019-10-11 20:41
 * @Version: 1.0
 */
@Slf4j
@Component
public class PreRequestFilter extends ZuulFilter {

    // 定义Filter的类型
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    // 定义Filter执行的顺序
    @Override
    public int filterOrder() {
        return 0;
    }

    // 是否需要执行这个过滤器
    @Override
    public boolean shouldFilter() {
        return true;
    }

    // Filter需要执行的具体操作
    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.set("startTime", System.currentTimeMillis());
        return null;
    }
}
